package com.atguigu.custmersystem.service;

import com.atguigu.custmersystem.javabean.Customer;

/**
 * @version 1.0
 * @Author 马冰
 * @Date 2024/11/11 9:51
 * @注释 电商客户管理系统后台展示
 */
public class CustomerService {
    //存储Customer对象
    private Customer[] cus;
    //记录客户的数量
    private int total = 0;

    //对数组进行初始化
    public CustomerService(int length) {
        cus = new Customer[length];
        Customer c1 = new Customer("张三", '男', 19, "15588546542", "zhangsan@guigu.com");
        Customer c2 = new Customer("李四", '男', 19, "15588546542", "zhangsan@guigu.com");
        Customer c3 = new Customer("王五", '男', 19, "15588546542", "zhangsan@guigu.com");
        addCustomer(c1);
        addCustomer(c2);
        addCustomer(c3);
    }

    /**
     * 添加客户对象到数组
     *
     * @param customer
     * @return
     */
    public boolean addCustomer(Customer customer) {
        if (total >= cus.length) {
            return false;
        }
        cus[total] = customer;
        total++;
        return true;
    }

    /**
     * 展示所有的客户信息
     *
     * @return
     */
    public Customer[] getAllCustomers() {

        Customer[] customers = new Customer[total];
        for (int j = 0; j < customers.length; j++) {
            customers[j] = cus[j];
        }
        return customers;
    }

    /**
     * 删除指定客户信息
     *
     * @param index
     * @return
     */
    public boolean deleteCustomer(int index) {
        if (index < 0 && index >= total) {
            return false;
        }
        for (int i = index; i < total - 1; i++) {
            cus[i] = cus[i + 1];
        }
        cus[total - 1] = null;
        total--;
        return true;
    }

    /**
     * 查询指定客户信息
     *
     * @param index
     * @return
     */
    public Customer getCustomer(int index) {
        if (index < 0 && index >= total) {
            return null;
        }
        return cus[index];
    }

    /**
     * 修改指定客户的信息
     *
     * @param index
     * @param cust
     * @return
     */
    public boolean replaceCustomer(int index, Customer cust) {
        if (index < 0 && index >= total) {
            return false;
        }
        cus[index] = cust;
        return true;
    }


}
